<?php

namespace MoChat\App\Call\Action\Dashboard\CallOmission;

use MoChat\App\Call\Constants\ClientTimeCode;
use MoChat\App\Call\Model\CallPlanWorkContact;
use MoChat\App\Call\Service\CallPlanService;
use MoChat\App\Call\Service\CallPlanWorkContactService;
use MoChat\App\Sales\Contract\SalesTaskContract;
use MoChat\Framework\Action\AbstractAction;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;

/**
 * 遗漏任务
 * @Controller
 */
class Index extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var CallPlanWorkContactService
     */
    protected $callPlanWorkContactService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     * })
     * @RequestMapping(path="/dashboard/callOmission/list", methods="get")
     */
    public function handle()
    {
        $user = user();
        // 判断用户绑定企业信息
        if (!isset($user['corpIds']) || count($user['corpIds']) !== 1) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未选择登录企业，不可操作');
        }
        $data = $this->request->all();
        $this->validated($data);

        $where = [];
        //客户名称
        if (isset($data['name']) && !empty($data['nane'])) {
            $where[] = ['client.b2b_client_name', 'like', '%' . $data['name'] . '%'];
        }
        //客服
        if (isset($data['service']) && !empty($data['service'])) {
            $where[] = ['client.b2b_manager_name', 'like', '%' . $data['service'] . '%'];
        }
        //业务员
        if (isset($data['salesman']) && !empty($data['salesman'])) {
            $where[] = ['client.b2b_salesman_name', 'like', '%' . $data['salesman'] . '%'];
        }

        //客户名称
        if (isset($data['name']) && !empty($data['nane'])) {
            $where[] = ['client.b2b_client_name', 'like', '%' . $data['name'] . '%'];
        }
        //任务状态
        if (isset($data['status']) && is_numeric($data['status'])) {
            $where[] = ['call_plan_work_contact.status', '=', $data['status']];
        }

        if (!empty($data['type'])) {
            if ($data['type'] == ClientTimeCode::TODAY) {
                $where[] = ['call_plan_work_contact.created_at', '>=', date('Y-m-d')];
                $where[] = ['call_plan_work_contact.created_at', '<=', date('Y-m-d 23:59:59')];
            }
            if ($data['type'] == ClientTimeCode::TOMORROW) {
                $where[] = ['call_plan_work_contact.created_at', '>=', date('Y-m-d', strtotime('+1 day'))];
                $where[] = ['call_plan_work_contact.created_at', '<=', date('Y-m-d 23:59:59', strtotime('+1 day'))];
            }

            if ($data['type'] == ClientTimeCode::OMISSION) {
                $where[] = ['call_plan_work_contact.is_omission', '=', 1];
            }
        }

        return $this->callPlanWorkContactService->getOmissionContract($where, [
            'call_plan_work_contact.id',
            'client.id as clientIds',//
            'client.b2b_client_name',//客户名称
            'client.b2b_user_type',//等级
            'client.enterprise_name',//单位名称
            'client.address',//地区
            'client.mobile',//手机号
            'client.b2b_manager_name',//客服
            'client.b2b_salesman_name',//业务员
//            'plan.plan_time',//任务时间
        'call_plan_work_contact.created_at as plan_time',//任务时间
            'plan.name',//计划名称
            'call_plan_work_contact.status',//任务状态
            'call_plan_work_contact.top_call_plan_id',//任务初始计划id
            'call_plan_work_contact.is_omission'//是否遗漏
        ]);
    }

    /**
     * 验证规则.
     */
    protected function rules(): array
    {
        return [
            'name' => 'max:30',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    protected function messages(): array
    {
        return [
            'name.max' => '客户名称长度不能超过30个字符',
        ];
    }
}
